package com.tomshushu.doc.freedoc.core.exception;

import cn.dev33.satoken.exception.NotLoginException;  // 导入 SaToken 原生异常类
import com.tomshushu.doc.freedoc.core.model.result.BaseResult;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理 SaToken 的未登录异常
     */
    @ExceptionHandler(NotLoginException.class)  // 确保与 SaToken 异常类一致
    public BaseResult<?> handleNotLoginException(NotLoginException e) {
        String errorMsg = "未登录，请先登录";
        // 根据异常类型细化提示（可选）
        if (e.getType() != null) {
            switch (e.getType()) {
                case NotLoginException.NOT_TOKEN:
                    errorMsg = "未提供 Token";
                    break;
                case NotLoginException.INVALID_TOKEN:
                    errorMsg = "Token 无效或已过期";
                    break;
                case NotLoginException.TOKEN_TIMEOUT:
                    errorMsg = "Token 已过期";
                    break;
            }
        }
        return BaseResult.failure(HttpStatus.UNAUTHORIZED.value(), errorMsg);
    }
}